import Vue from 'vue';
import Vuex from 'vuex';
import lodash from 'lodash';
import { getLabelData } from '@/api/openApi';
import { getAuthMenuData } from '@/api/loginRegister';
import { queryAttachPath } from '@/api/system/tbSysAttachment';
import { queryClassifyList } from '@/api/basedata/sysClassify';
import { siteBaseInfo, getDictConstByCode, getDictEnumByCode } from '@/api/openApi';
Vue.use(Vuex)

const state = {
	token: '',
	siteInfo: {},
	userInfo: {},
	dictData: {},
	menuData: [],
	authButton: [],
	classifyData: []
}

const actions = {
	init: ({ getters, dispatch }) => {
		return new Promise(async (resolve, reject) => {
			await dispatch('judgeSiteGray');
			await dispatch('getSiteBaseInfo');
			resolve();
		}).catch(() => {
			reject();
		})
	},

	//获取站点基本信息
	async getSiteBaseInfo({ commit, dispatch }) {
		let result = await siteBaseInfo().then(res => res.data);
		if (result.code === '0') {
			let favicon = document.getElementById('favicon');
			let datas = Vue.prototype.$detailData(result.data);
			if (!!datas.webLogo) {
				datas['webLogoPath'] = await dispatch('getFileUrl', datas['webLogo']);
			}
			if (!!datas.userPic) {
				datas['userPicPath'] = await dispatch('getFileUrl', datas['userPic']);
			}
			if (!!datas.webLogo) {
				datas['faviconPath'] = await dispatch('getFileUrl', datas['field3']);
			}
			commit('SET_SITE_INFO', datas);
			favicon.href = datas.faviconPath.replaceIP();
			document.title = datas.name;
		}
	},

	//网站是否灰掉
	async judgeSiteGray({ dispatch, commit }) {
		let grayValue = await dispatch('getConstByCode', 'GRAY_SITE');
		if (grayValue === '1') {
			document.body.style.filter = 'grayscale(1)';
		} else {
			document.body.removeAttribute('style');
		}
	},

	//通过CODE获取字典常量数据
	async getConstByCode({ commit }, code) {
		let result = await getDictConstByCode(code).then(res => res.data);
		if (result.code === '0') {
			let data = result.data || [];
			let datas = data.length > 0 ? data[0] : '';
			return Promise.resolve({ code, value: datas });
		}
	},

	//通过CODE获取字段枚举数据
	async getEnumByCode({ commit }, code) {
		let result = await getDictEnumByCode(code).then(res => res.data);
		if (result.code === '0') {
			return Promise.resolve({ code, value: result.data || [] });
		}
	},

	//获取权限菜单
	async getAuthMenu({ commit }, id) {
		let result = await getAuthMenuData(id).then(res => res.data);
		if (result.code === '0') {
			let datas = Vue.prototype.$detailData(result.data);
			let { menuToRouter } = require('@/utils/getRouter');
			commit('SET_MENU_DATA', new menuToRouter(datas.menu));
			commit('SET_AUTH_BUTTON', datas.button);
		}
	},

	//获取系统类型数据
	async getSystemClassify({ commit }) {
		let params = { page: 1, limit: 9999, status: 1 }
		let result = await queryClassifyList(params).then(res => res.data);
		if (result.code === '0') {
			commit('SET_CLASSIFY_DATA', result.data || []);
		}
	},

	//获取附件地址
	async getFileUrl({ commit }, id) {
		let result = await queryAttachPath(id).then(res => res.data);
		if (result.code === '0') {
			let datas = Vue.prototype.$detailData(result.data);
			return Promise.resolve(lodash.isEmpty(datas) ? '' : datas);
		}
	}
}

const mutations = {
	SET_TOKEN: (state, d) => {
		state.token = d;
		Vue.prototype.$session.set('token', d);
	},
	SET_SITE_INFO: (state, d) => {
		state.siteInfo = d;
		Vue.prototype.$local.set('siteInfo', d);
	},
	SET_USER_INFO: (state, d) => {
		state.userInfo = d;
		Vue.prototype.$local.set('userInfo', d);
	},
	SET_MENU_DATA: (state, d) => {
		state.menuData = d;
		Vue.prototype.$local.set('menuList', d);
	},
	SET_AUTH_BUTTON: (state, d) => {
		state.authButton = d;
		Vue.prototype.$local.set('authButton', d);
	},
	SET_CLASSIFY_DATA: (state, d) => {
		state.classifyData = d;
		Vue.prototype.$local.set('classifyData', d);
	}
}

export default { state, actions, mutations }